local printx = debug.printx
local AOI_ACTION_TYPE = AOI_ACTION_TYPE

clsSceneUser = ALL_CLASS.clsSceneObj:Inherit("clsSceneUser")

function __init__(self)
    -- 定制AOI范围
	self.clsSceneUser:RegTmpVar("AoiRange")
end

function clsSceneUser:OnCreate(oci)
    Super(clsSceneUser).OnCreate(self, oci)

    self.m_AoiMask = AOI_MASK_USER

	--当前可见对象，记录自己目前可以看见的单位
	self._VisibleUids = {}
	self._VisibledUids = {}	--当前被哪些对象可见

	-- 进出AOI需要同步的玩家
	self._UserEnterList = {}
	self._UserLeaveList = {}
end

function clsSceneUser:ToString()
	return string.format("Uid=%s, SceneID=%d, SceneUid=%d",
		self:GetUid() or 0, self:GetSceneID() or 0, self:GetSceneUid() or 0)
end

function clsSceneUser:GetAoiMask()
	return self.m_AoiMask
end

-- function clsSceneUser:OnEnterScene(pos, scene, extra)
-- 	Super(clsSceneUser).OnEnterScene(self, pos, scene)
-- end

function clsSceneUser:OnLeaveScene(scene)
    local sceneID = scene:GetSceneId()
    local sceneUid = scene:GetSceneUid()
	
	-- 离开场景时，同步内容可以清空
	self._UserEnterList = {}
	self._NpcEnterList = {}
	
	Super(clsSceneUser).OnLeaveScene(self, scene)
end

function clsSceneUser:OnObjEnter(targetObj)
	local targetUid = targetObj:GetUid()
    self:OnVisibleEnter(targetUid, targetObj)
end

function clsSceneUser:OpenSyncFlag()
	-- if not ALLSCENE:AddUser2Sync(self:GetUid()) then
	-- 	self:OnTimeSync()
	-- end
end

--视野控制，obj进入自己的视野
function clsSceneUser:OnVisibleEnter(targetUid, targetObj)
	if targetObj == self then return end
	local uid = self:GetUid()
	if targetUid == uid then return end
	if not self._VisibleUids[targetUid] then
		self._VisibleUids[targetUid] = true
	end
	targetObj._VisibledUids[uid] = self:GetVfd()
	self._UserLeaveList[targetUid] = nil
	self._UserEnterList[targetUid] = true
	self:OpenSyncFlag()
end

function clsSceneUser:OnObjLeave(targetObj)
    local targetUid = targetObj:GetUid()
    self:OnVisibleLeave(targetUid, targetObj)
end

function clsSceneUser:OnVisibleLeave(targetUid, targetObj)
	-- print("OnVisibleLeave", self, targetObj)
	if targetObj == self then return end
	local uid = self:GetUid()
	if targetUid == uid then return end
	if self._VisibleUids[targetUid] then
		self._VisibleUids[targetUid] = nil
	end
	targetObj._VisibledUids[uid] = nil
	self._UserEnterList[targetUid] = nil
	self._UserLeaveList[targetUid] = 1
	self:OpenSyncFlag()
end